package com.lvw.hadoop.spring;

import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 使用 Spring Hadoop 操作 Hadoop 文件系统
 * 使用到的文件: hadoop.xml, application.properties
 */
public class SpringDataHadoopAPP {

    ApplicationContext ctx;
    FileSystem fileSystem;

    @Before
    public void setUp() {
        ctx = new ClassPathXmlApplicationContext("hadoop.xml");
        fileSystem = ((FileSystem) ctx.getBean("fileSystem"));
    }

    @After
    public void tearDown() {
        ctx = null;
    }

    @Test
    public void testMkdir() throws Exception {
        fileSystem.mkdirs(new Path("/springDir"));
    }

    @Test
    public void testRead() throws Exception {
        FSDataInputStream open = fileSystem.open(new Path("/hello.txt"));

        IOUtils.copyBytes(open, System.out, 1024);
    }
}
